package net.xo68.coordinator.client;


import com.alipay.sofa.jraft.entity.PeerId;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Slf4j
public class CoordinatorClientApp {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        CoordinatorClientConfig clientConfig=new CoordinatorClientConfig();
        clientConfig.loadFromFile("wuxie-coordinator-client/config/server.properties");

        final CoordinatorClient client=new CoordinatorClient(clientConfig);
        client.start();

        PeerId leader=client.getLeader();
        log.info("leader: {}", leader);



        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                client.shutdown();
                log.info("client shutdown {}", LocalDateTime.now());
            }
        });
        for(long i=0L;i<100000L;i++){

            Random random=new Random(i);
            long key=random.nextInt(5)+1L;
            long id=client.getNewId(key);

            log.info("key:{},newId: {},leader: {}", key, id,client.getLeader());

            TimeUnit.SECONDS.sleep(1);
        }
    }
}
